/*
 * @Author: cola(sunshiwen)
 * @Date: 2022-04-19 09:11:44
 * @LastEditTime: 2022-05-20 14:56:03
 * @LastEditors: cola(sunshiwen)
 * @Description: account controller 
 * @FilePath: /maycur-koa/controller/accountCtrl.js
 */
'use strict';

const Sequelize = require('sequelize');
const C = require('../common/constant');
const accountModel = require('../model/accountModel');

const { Op } = Sequelize;

module.exports = {
    /**
     * 获取全部账号信息
     */
    fetchAllAccount: async ctx => {
        const data = await accountModel.findAll({ where: { invalid: C.CHAR.NO } });

        ctx.body = data;
    },

    /**
     * 获取账号信息
     */
    fetchAccount: async ctx => {
        const { env } = ctx.reqParams.router;

        const data = await accountModel.findAll({ where: { env, invalid: C.CHAR.NO } });

        ctx.body = data;
    },

    /**
     * 添加账号信息
     */
    addAccount: async ctx => {
        const { account, env, name, searchKey } = ctx.reqParams.body;

        const data = await accountModel.findOne({ where: { account, invalid: C.CHAR.NO } });
        if (data) {
            throw new Error('账号已存在');
        }

        const list = await accountModel.create({ account, env, name, searchKey });

        ctx.body = list;
    },

    /**
     * 修改账号信息
     */
    updateAccount: async ctx => {
        const { id } = ctx.reqParams.router;
        const { account, env, name, searchKey } = ctx.reqParams.body;

        const data = await accountModel.findOne({ where: { account, invalid: C.CHAR.NO, id: { [Op.ne]: id } } });
        if (data) {
            throw new Error('账号已存在');
        }
        await accountModel.update({ account, env, name, searchKey }, { where: { id, invalid: C.CHAR.NO } });

        ctx.body = 'SUCCESS';
    },

    /**
     * 删除账号信息（软删除）
     */
    deleteAccount: async ctx => {
        const { id } = ctx.reqParams.router;

        const data = await accountModel.update({ invalid: C.CHAR.YES }, { where: { id, invalid: C.CHAR.NO } });
        if (data <= 0) {
            throw new Error('删除失败');
        }
        ctx.body = 'SUCCESS';
    },
};
